package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PermissionServiceImpl implements IPermissionService {
    private PermissionMapper permissionMapper;

    @Autowired
    public void setPermissionMapper(PermissionMapper permissionMapper) {
        this.permissionMapper = permissionMapper;
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Permission permission, Long[] ids) {
        permissionMapper.insert(permission);
//        if (ids != null && ids.length > 0) {
//            for (Long rid : ids) {
//                permissionMapper.insertRelation(permission.getId(), rid);
//            }
//        }
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void update(Permission permission, Long[] ids) {
        permissionMapper.updateByPrimaryKey(permission);
        //删除关系表
//        permissionMapper.deleteRelation(permission.getId());
//        if (ids != null && ids.length > 0) {
//            for (Long rid : ids) {
//                permissionMapper.insertRelation(permission.getId(), rid);
//            }
//        }
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectAll();
        return new PageInfo<>(permissions);
    }

    @Autowired
    private ApplicationContext ctx;

    @Override
    public void reload() {
        List<String> permissions = permissionMapper.selectAllExpression();
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> beans = map.values();
        for (Object bean : beans) {
            Class<?> aClass = bean.getClass();
            Method[] methods = aClass.getDeclaredMethods();
            for (Method method : methods) {
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                if (annotation == null) {
                    continue;
                }
                String name = annotation.name();
                String expression = annotation.expression();
                if (!permissions.contains(expression)) {
                    Permission permission = new Permission();
                    permission.setName(name);
                    permission.setExpression(expression);
                    permissionMapper.insert(permission);
                    permissions.add(expression);
                }
            }
        }

    }

    @Override
    public List<Permission> selectByRoleId(Long id) {
        List<Permission> permissions = permissionMapper.selectByRoleId(id);
        return permissions;
    }

    @Override
    public List<String> queryPermissionByEmpId(Long id) {
        return permissionMapper.queryPermissionByEmpId(id);
    }

}
